package com.yphone.net.helper;

import android.os.Handler;
import android.os.Looper;

import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpClientMethod {
    private static OkHttpClientMethod mInstance = null;
    private OkHttpClient mOkHttpClient;

    public interface Callback2 {
        void onFailure(Call call, IOException iOException);

        void onResponse(Call call, byte[] bArr) throws IOException;
    }

    public class CallbackToMainThread implements Callback {
        /* access modifiers changed from: private */
        public byte[] mBody = null;
        /* access modifiers changed from: private */
        public Call mCall = null;
        /* access modifiers changed from: private */
        public Callback2 mCallback = null;
        /* access modifiers changed from: private */
        public IOException mIOException = null;
        Runnable runnableFailureUI = new Runnable() {
            public void run() {
                CallbackToMainThread.this.mCallback.onFailure(CallbackToMainThread.this.mCall, CallbackToMainThread.this.mIOException);
            }
        };
        Runnable runnableSuccessUI = new Runnable() {
            public void run() {
                try {
                    CallbackToMainThread.this.mCallback.onResponse(CallbackToMainThread.this.mCall, CallbackToMainThread.this.mBody);
                } catch (IOException e) {
                    CallbackToMainThread.this.mCallback.onFailure(CallbackToMainThread.this.mCall, e);
                }
            }
        };

        public CallbackToMainThread(Callback2 callback) {
            this.mCallback = callback;
        }

        public void onFailure(Call call, IOException e) {
            this.mCall = call;
            this.mIOException = e;
            new Handler(Looper.getMainLooper()).post(this.runnableFailureUI);
        }

        public void onResponse(Call call, Response response) {
            this.mCall = call;
            try {
                this.mBody = response.body().bytes();
                new Handler(Looper.getMainLooper()).post(this.runnableSuccessUI);
            } catch (IOException e) {
                this.mIOException = e;
                new Handler(Looper.getMainLooper()).post(this.runnableFailureUI);
            }
        }
    }

    private static class TrustAllCerts implements X509TrustManager {
        private TrustAllCerts() {
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }

    private OkHttpClientMethod() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(30, TimeUnit.SECONDS);
        builder.connectTimeout(30, TimeUnit.SECONDS);
        builder.writeTimeout(30, TimeUnit.SECONDS);
        builder.sslSocketFactory(createSSLSocketFactory());
        builder.hostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        this.mOkHttpClient = builder.build();
    }

    private static SSLSocketFactory createSSLSocketFactory() {
        SSLSocketFactory ssfFactory = null;
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new TrustAllCerts()}, new SecureRandom());
            return sc.getSocketFactory();
        } catch (Exception e) {
            return ssfFactory;
        }
    }

    public static OkHttpClientMethod getInstance() {
        if (mInstance == null) {
            synchronized (OkHttpClientMethod.class) {
                if (mInstance == null) {
                    mInstance = new OkHttpClientMethod();
                }
            }
        }
        return mInstance;
    }

    private Headers setHeaders(Map<String, String> headersParams) {
        Headers.Builder headerBuilder = new Headers.Builder();
        if (headersParams != null) {
            for (String key : headersParams.keySet()) {
                headerBuilder.add(key, (String) headersParams.get(key));
            }
        }
        return headerBuilder.build();
    }

    private String setUrlParams(Map<String, String> params) {
        StringBuffer param = new StringBuffer();
        int i = 0;
        if (params == null) {
            return param.toString();
        }
        for (String key : params.keySet()) {
            if (i == 0) {
                param.append("?");
            } else {
                param.append("&");
            }
            param.append(key).append("=").append((String) params.get(key));
            i++;
        }
        return param.toString();
    }

    private RequestBody setPostBody(Map<String, String> params) {
        if (params.containsKey("img")) {
            MultipartBody.Builder formBodyBuilder = new MultipartBody.Builder();
            formBodyBuilder.setType(MediaType.parse("multipart/form-data; charset=utf-8"));
            if (params != null) {
                for (String key : params.keySet()) {
                    if (!key.equals("img")) {
                        formBodyBuilder.addFormDataPart(key, (String) params.get(key));
                    }
                }
                String key2 = "img";
                String path = (String) params.get(key2);
                formBodyBuilder.addFormDataPart(key2, path.substring(path.lastIndexOf(47) + 1), RequestBody.create(MediaType.parse("image/*"), new File(path)));
            }
            return formBodyBuilder.build();
        }
        FormBody.Builder formBodyBuilder2 = new FormBody.Builder();
        if (params != null) {
            for (String key3 : params.keySet()) {
                formBodyBuilder2.add(key3, (String) params.get(key3));
            }
        }
        return formBodyBuilder2.build();
    }

    public void getAsyncMethod(String url, Map<String, String> urlParams, Map<String, String> headerParams, Callback2 okHttpRequestCallBack) {
        try {
            this.mOkHttpClient.newCall(new Request.Builder().url(url + setUrlParams(urlParams)).headers(setHeaders(headerParams)).get().build()).enqueue(new CallbackToMainThread(okHttpRequestCallBack));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void postAsyncMethod(String url, Map<String, String> urlParams, Map<String, String> bodyParams, Map<String, String> headerParams, Callback2 okHttpRequestCallBack) {
        try {
            this.mOkHttpClient.newCall(new Request.Builder().url(url + setUrlParams(urlParams)).headers(setHeaders(headerParams)).post(setPostBody(bodyParams)).build()).enqueue(new CallbackToMainThread(okHttpRequestCallBack));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}